import Vue from 'vue'
import Main from './main.vue'

let MessageConstructor = Vue.extend(Main)
let instance
let time
let timer
let renderMessage = false

const Message = function(options) {
    clearTimeout(timer)
    options = options || {};
    time = options.options || 3000;
    if (typeof options === 'string') {
      options = {
        text: options,
        type: 'warn'
      };
    }
    if(!renderMessage){
      instance = new MessageConstructor();
      instance.vm = instance.$mount()
      document.body.appendChild(instance.vm.$el)
    }
    renderMessage = true
    instance.text = options.text
    instance.type = options.type

    timer = setTimeout(()=> {
      instance.show = false
    }, time)
    
    instance.show = true
    instance.dom = instance.vm.$el;

    return instance.vm
}

export default Message;